//分享页
declare var $:any;
declare var wx: any;
declare var WeixinJSBridge : any;
import { Component,OnInit } from '@angular/core';
// import { Router } from '@angular/router';
import { UserComponent } from '../../components/user/user.component';

import { stampDataService } from '../../services/stampData.service';
import { stampProgressService } from '../../services/stampProgress.service';
import { isWeixin,isFeixin } from "../../services/browser.service";
import { encry,decry } from "../../services/base64.service";
@Component({
  selector: 'report',
  templateUrl: './report.component.html',
  styleUrls: ['./report.component.css']
})
export class ReportComponent {
    title = 'app';
    constructor(
    	// private router: Router,
    	private StampDataService:stampDataService,
        private stampProService:stampProgressService
    ){}

    //邮戳详情弹窗
    stampHide = true;
    //分享遮罩层
    shareHide = true;
    //分享按钮
    shareBtnHide = false;

    //服务器资源头
    imgPre = "../../wxassets/twoyear/";

    //彩带图片
    ribbonImg = this.imgPre + "assets/images/ribbon.png";

    //邮戳列表
    stampList = [
        {
            imgSrc:this.imgPre + "assets/images/logo/1_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/2_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/3_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/4_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/5_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/6_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/7_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/8_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/9_s_g.png",
            own:0
        },
        {
            imgSrc:this.imgPre + "assets/images/logo/10_s_g.png",
            own:0
        }
    ];

    //邮戳个数
    stamp_num = 10;

    //用户信息
    user_name = "";
    user_phone = "";
    ngOnInit(){

        var self = this;

        //分享按钮
        self.checkShareBtn();

        //解密手机号和姓名
        self.user_phone = decry(window.location.href.split("?")[2].split("&")[0]);
        self.user_name = decry(window.location.href.split("?")[2].split("&")[1]);

        //动画
        self.animation();

        //获取用户集邮进度显示
        self.stampProService.getStampPro(localStorage.getItem("phone")).then((res)=>{
            console.log(res);
            if(res.data.code == 0){
                
                var list = res.data.msg;
                var count = 0;
                var i = 0;
                for(let k in list){
                    let n = i+1;
                    
                    if(list[k]!=""){
                        self.stampList[i].imgSrc = this.imgPre + "assets/images/logo/"+n+"_s.png";
                        self.stampList[i].own = 1;
                        count++;
                    }else{
                        self.stampList[i].imgSrc = this.imgPre + "assets/images/logo/"+n+"_s_g.png";
                        self.stampList[i].own = 0;
                    }
                    i++;
                }
                self.stamp_num = count;

            }
        }).catch((err)=>{
           alert("出错啦！"); 
        });


        var info = {
            url : window.location.href
        }

        $.post('http://yxbsve.mmarket.com/index.php?c=view&a=public_mmshow_share',info,function(data){
             //gola = shallowCopy(data);

            wx.config({
                debug: false,
                appId: data.appId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage'
                ]
            });
            wx.ready(function() {
                function shareData64() {

                    var shareData21 = {
                        title: "中移互联网小镇集邮活动",
                        desc: "中移互联网小镇集邮活动",
                        link: window.location.href,
                        imgUrl: "http://yxbsve.mmarket.com/wxassets/twoyear/assets/images/logo/1_s.png",
                        success: function(res) {
                            alert(res)
                        },
                        fail:function(res){
                            alert(res)
                        },
                        error:function(res){
                            alert(res);
                        }   
                    };

                    return shareData21;
                }

            
                wx.onMenuShareAppMessage(shareData64()); //分享给朋友
                wx.onMenuShareTimeline(shareData64()); //分享到朋友圈
                
            });


            wx.error(function(res) {
            alert(res.errMsg)
            });
        })
    }

    checkShareBtn(){
        if(!isWeixin(navigator.userAgent.toLowerCase())){
            this.shareBtnHide = true;
        }
    }
    

    //分享按钮
    share_btn(){
        this.shareHide = false;
    }

    //邮戳详情相关信息
    stamp_name = "";
    stamp_src = "";
    stamp_desc = ""
    stamp_link = "";
    linkHide = false;
    shareImg = this.imgPre + "assets/images/shareArrow.png";

    //查看邮戳函数
    checkStamp(i){
        var stampData = this.StampDataService.getStampInfo(i);
        this.stamp_name = stampData.name;
        this.stamp_src = stampData.logoSrc;
        this.stamp_desc = stampData.desc;
        if(stampData.link == ""){
            this.linkHide = true;
        }else{
            this.stamp_link = stampData.link;
        }
    	this.stampHide = false;
    }

    //关闭邮戳详情
    closeStamp(){
        this.stampHide = true;
    }

    //动画效果
    animation(){
        $(".ribbonImg").enter("zoomIn");
        $(".titleBox").enter("zoomIn");
    }

    //关闭分享遮罩层
    closeShare(){
        this.shareHide = true;
    }
}
